home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / IUNDECL.ICN < prev    next >
Text File  |  1992-11-26  |  4KB  |  112 lines

  1. ############################################################################
  2. #
  3. #    File:     iundecl.icn
  4. #
  5. #    Subject:  Program to find undeclared Icon identifiers
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     May 28, 1992
  10. #
  11. ###########################################################################
  12. #
  13. #  This program invokes icont to find undeclared variables in an Icon
  14. #  source program.  The output is in the form of a "local" declaration,
  15. #  preceded by a comment line that identifies that procedure and file
  16. #  name from whence it arose.  Beware that undeclared variables aren't
  17. #  necessarily local, so any which are intended to be global must be
  18. #  removed from the generated list.
  19. #
  20. #  Multiple files can be specified as arguments, and will be processed
  21. #  in sequence.  A file name of "-" represents the standard input file.
  22. #  If there are no arguments, standard input is processed.
  23. #
  24. #  The program works only if procedures are formatted such that the
  25. #  keywords "procedure" and "end" are the first words on their
  26. #  respective lines.
  27. #
  28. #  Only for UNIX, since the "p" (pipe) option of open() is used.
  29. #
  30. ############################################################################
  31. #
  32. #  Requires: UNIX
  33. #
  34. ############################################################################
  35.  
  36. link filename
  37.  
  38. procedure main(arg)
  39.    local f, fn, line, names, p, sep, t, argstring, undeclared, pn
  40.    #
  41.    #  Process command line file names.
  42.    #
  43.    if *arg = 0 then arg := ["-"] # if no arguments, standard input
  44.    #
  45.    #  Build a set of all the undeclared identifiers.
  46.    #
  47.    argstring := ""
  48.    every argstring ||:= " " || !arg
  49.    p := open("icont -s -u -o /dev/null 2>&1" || argstring,"p") |
  50.        stop("popen failed")
  51.    undeclared := set()
  52.    while line := read(p) do line ?
  53.      if find("undeclared identifier") then
  54.            tab(find("\"") + 1) & insert(undeclared,tab(find("\"")))
  55.    close(p)
  56.    #
  57.    #  Loop through files to process individual procedures.
  58.    #
  59.    every fn := !arg do {
  60.       f := if fn == "-" then &input else {
  61.      fn := \suffix(fn)[1] || ".icn"
  62.      open(fn) | stop("Can't open input file \"",fn,"\"")
  63.      }
  64.       #
  65.       #  Loop to process lines of file (in string scanning mode).
  66.       #
  67.       while line := read(f) do line ? {
  68.      if tab(many(' \t')) | "" & ="procedure" & tab(many(' \t')) then {
  69.         t := open("undeclared_tmp.icn","w") | stop("Can't open work file")
  70.         write(t,line)
  71.         while line := read(f) do line ? {
  72.            write(t,line)
  73.            if tab(many(' \t')) | "" & ="end" & many(' \t') | pos(0) then
  74.              break
  75.            }
  76.         close(t)
  77.         #
  78.             #  Now we have an isolated Icon procedure -- invoke icont to
  79.         #  determine its undeclared variables.
  80.         #
  81.         p := open("icont -s -u -o /dev/null 2>&1 undeclared_tmp.icn","p") |
  82.           stop("popen failed")
  83.         names := []
  84.         while line := read(p) do line ?
  85.           if find("undeclared identifier") then
  86.             tab(find("\"") + 1) &
  87.             put(names,member(undeclared,tab(find("\""))))
  88.         close(p)
  89.         #
  90.         #  Output the declaration.
  91.         #
  92.         pn := "\"" || tab(upto(' \t(')) || "\"" ||
  93.           if *arg > 1 then " (" || fn || ")" else ""
  94.         if *names = 0 then write("# ",pn," is OK")
  95.         else {
  96.            write("# Local declarations for procedure ",pn)
  97.            sep := "   local "
  98.            every writes(sep,!sort(names)) do sep := ", "
  99.            write()
  100.            }
  101.         }
  102.      }
  103.       #
  104.       #  Close this input file.
  105.       #
  106.       close(f)
  107.       }
  108.    remove("undeclared_tmp.icn")
  109. end
  110.  
  111.  
  112.